package com.titan.demo.camel.sample.Expression;

import java.util.HashMap;
import java.util.Map;

import junit.framework.Assert;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.language.simple.SimpleLanguage;

public class TestSimple01 {
	public static void main(String[] args) {
        try {
        	CamelContext context = new DefaultCamelContext();
            Exchange exchange = new DefaultExchange(context);
            exchange.getIn().setHeader("h1", "v1");
            exchange.getIn().setHeader("h2", "v2");
            exchange.getIn().setBody("火狼");
        	String template = "这个模板|${body}|${header.h1}|${headers.h2}";
        	Expression exp = SimpleLanguage.simple(template, String.class);
//        	Expression exp = context.resolveLanguage("simple").createExpression(template);
        	Assert.assertEquals("这个模板|火狼|v1|v2", exp.evaluate(exchange, String.class));
        	
        	
        	exchange.getIn().setHeader("hint", "123");
        	exp = SimpleLanguage.simple("${header.hint} == 123", boolean.class);
        	Assert.assertEquals(Boolean.TRUE, exp.evaluate(exchange, Object.class));
        	
        	Map<String,Object> map = new HashMap<String,Object>();
        	map.put("array", new Integer[]{1,2,3});
        	exchange.getIn().setBody(map);
        	exp = SimpleLanguage.simple("${body[array]}", Integer[].class);
        	Assert.assertEquals(2, ((Integer[])exp.evaluate(exchange, Integer[].class))[1].intValue());
        	
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
}
